package com.dongdongsongcan.app.activity;

import android.content.Context;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import com.dongdongsongcan.app.R;
import com.dongdongsongcan.app.control.OrderControl;
import com.dongdongsongcan.app.control.bean.OrderStatusData;
import com.dongdongsongcan.app.lib.pulltorefresh.PullToRefreshBase;
import com.dongdongsongcan.app.lib.pulltorefresh.PullToRefreshScrollView;
import com.dongdongsongcan.app.view.OrderStatusFoodItem;
import com.dongdongsongcan.app.view.OrderStatusItem;
import com.su.dolphin.control.task.CtrlTask;
import com.su.dolphin.utils.mgr.DpActivityManager;
import com.su.dolphin.view.activity.BaseActivity;

import java.util.List;

public class OrderStatusActivtiy extends BaseActivity {

    private String mOrderId;
    private LinearLayout mOrderstatus_container;
    private PullToRefreshScrollView orderstatus_scrollview;

    public static void startFrom(Context context, String orderId) {
        Bundle bundle = new Bundle();
        bundle.putString("orderId", orderId);
        DpActivityManager.i().startActivity(context, OrderStatusActivtiy.class, bundle);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mOrderId = this.getIntent().getExtras().getString("orderId");
        setContentView(R.layout.activity_orderstatus);
    }


    @Override
    public void initView() {
        mOrderstatus_container = (LinearLayout) findViewById(R.id.orderstatus_container);
        orderstatus_scrollview = (PullToRefreshScrollView) findViewById(R.id.orderstatus_scrollview);
    }

    @Override
    public void setListener() {
        orderstatus_scrollview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                getOrderStatus();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {

            }
        });
    }

    @Override
    public void initData() {
        getOrderStatus();
    }

    public void getOrderStatus() {
        OrderControl.order_status(mOrderId, new CtrlTask.IControlResponse<OrderStatusData>() {
            @Override
            public void onControlResponse(OrderStatusData data) {
                orderstatus_scrollview.onRefreshComplete();
                if (data.isOk()) {
                    List<OrderStatusData.Food> foods = data.getResult();
                    if (foods.isEmpty()) {
                        return;
                    }
                    mOrderstatus_container.removeAllViews();

                    for (int j = 0; j < foods.size(); j++) {
                        OrderStatusFoodItem orderStatusFoodItem = new OrderStatusFoodItem(OrderStatusActivtiy.this);
                        orderStatusFoodItem.setInfo(foods.get(j));
                        mOrderstatus_container.addView(orderStatusFoodItem.getView());
                    }

                    List<OrderStatusData.Status> statuses = data.getLogs();
                    for (int i = 0; i < statuses.size(); i++) {
                        OrderStatusItem orderStatusItem = new OrderStatusItem(OrderStatusActivtiy.this);
                        orderStatusItem.setInfo(statuses.get(i));
                        mOrderstatus_container.addView(orderStatusItem.getView());
                    }

                }

            }
        });
    }

}
